const formatTime = (date = new Date()) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return (
    [year, month, day].map(formatNumber).join("-") +
    " " +
    [hour, minute, second].map(formatNumber).join(":")
  );
};

const formatTime_yymmdd = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return [year, month, day].map(formatNumber).join("-");
};
const formatTime_yymm = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  return [year, month].map(formatNumber).join("-");
};
const formatTime_hhmmss = (date) => {
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return [hour, minute, second].map(formatNumber).join(":");
};

const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};

const now = (date) => {
  return formatTime(new Date());
};

const promisify = (original) => {
  return function (opt) {
    return new Promise((resolve, reject) => {
      opt = Object.assign(
        {
          success: resolve,
          fail: reject,
        },
        opt
      );
      original(opt);
    });
  };
};

const regEx = {
  Percent: /^(0)$|^100$|^[1-9][0-9]?$/, // 大于等于0小于等于100正数
  num: /^[+]{0,1}(\d+)$/, // 正整数的正则表达式(包括0)
  numno0: /^[1-9]\d*$/, // 正整数的正则表达式(不包括0)
  Strs: /^[\u0391-\uFFE5\w]+$/, //中文字、英文字母、数字和下划线
  Tel: /^1[3456789]\d{9}$/, //更新最新手机验证
  SpecialWord: /^[`\\~\!\@\#\$\%\^\&\*\(\)\_\+\{\}\|\:\"\<\>\?\/\.\,\;\'\[\]\\]+$/, //检查特殊字符
  passWord: /^(?![^a-zA-Z]+$)(?!\D+$)/,
  price: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
};

const isRegEx = function (str, regEx) {
  return regEx.test(str);
};
const isPercent = function (str) {
  return regEx.Percent.test(str);
};
const isNum = function (str) {
  return regEx.num.test(str);
};
const isNumber = function (str) {
  // 大于0 正整数
  return regEx.num.test(str);
};
const isPrice = function (str) {
  return regEx.price.test(str);
};
const isEmpty = function (str) {
  return str === "" || str === null || str === undefined || str.length === 0
    ? true
    : false;
};
const notEmpty = function (str) {
  return str === "" || str === null || str === undefined || str.length === 0
    ? false
    : true;
};
/**
 * @param {*} args 参数为空值 返回true，并提示空信息
 *                 参数为数组或对象 遍历每一项是否为空 返回true，并提示空信息
 * @param {*} tip_msg
 */
const tipEmpty = function (args, tip_msg) {
  if (typeof args === "object") {
    // 数组
    if (args.length === 0) {
      wx.showToast({
        title: tip_msg || "请正确填写信息",
        icon: "none",
      });
      return true;
    }
    for (let index = 0; index < args.length; index++) {
      console.warn(args[index], !isEmpty(args[index]));
      if (isEmpty(args[index])) {
        wx.showToast({
          title: tip_msg || "请正确填写信息",
          icon: "none",
        });
        return true;
      } else if (index == args.length - 1) {
        return false;
      }
    }
  } else {
    // 单个参数
    if (isEmpty(args)) {
      wx.showToast({
        title: tip_msg || "请正确填写信息",
        icon: "none",
      });
      return true;
    } else {
      return false;
    }
  }
};
/**
 * @param {*} bool 参数为boolen判断 返回true，并提示空信息
 * @param {*} tip_msg
 */
const tipRight = function (bool, tip_msg) {
  if (bool == true) {
    wx.showToast({
      title: tip_msg || "请正确填写信息",
      icon: "none",
    });
    return true;
  } else {
    return false;
  }
};
const tipFalse = function (bool, tip_msg) {
  return tipRight(!bool, tip_msg);
};

const chkSpcWord = function (str) {
  if (regEx.SpecialWord.test(str)) {
    return true;
  } else {
    return false;
  }
};
const chkPassWord = function (str) {
  if (regEx.passWord.test(str)) {
    return true;
  } else {
    return false;
  }
};
//检查手机号
const chkTel = function (str) {
  console.log(str, regEx.Tel);
  var reg = regEx.Tel;
  if (reg.test(str)) {
    return true;
  } else {
    return false;
  }
};
//提示语
const showToast = function (title = "", mask = true, type, cb) {
  wx.showToast({
    title: title + "",
    icon: type == "ok" ? "success" : "none",
    mask: mask,
    duration: 2000,
  });
  if (cb) {
    setTimeout(() => {
      cb();
    }, 1500);
  }
};

// 空格分隔 字符串 返回 数组
// 数组为空返回 undifined
// splitStr(sss) || []
const splitStr = function (str = "") {
  if (str instanceof Array) {
    return str;
  }
  let arr = str
    .trim()
    .replace(/\n|,|，|\;|。|；|\/|、|\|/g, " ")
    .trim()
    .split(" ")
    .filter((s) => s && s.trim());
  if (arr.length === 0) {
    return undefined;
  }
  return arr;
};

var util = (module.exports = {
  regEx,

  isRegEx,
  isPercent,
  isNum,
  isNumber,
  isPrice,
  isEmpty,
  notEmpty,
  tipEmpty,
  tipRight,
  tipFalse,
  chkSpcWord,
  chkPassWord,
  chkTel,
  formatNumber,
  formatTime,
  formatTime_yymmdd,
  formatTime_yymm,
  formatTime_hhmmss,
  now,
  promisify,
  splitStr,
});
